#ifndef T1_DAC_H_
#define T1_DAC_H_

#include "regs.h"
#include "iodef.h"

typedef struct {
	__IO uint32_t TXREG; /* DAC transmit data register */
	__IO uint32_t GCR;   /* DAC global control register */
	__IO uint32_t ISR;   /* DAC interrupt state register */
	__IO uint32_t IER;   /* DAC interrupt enable register */
	__IO uint32_t ICR;   /* DAC interrupt clear register */
	__IO uint32_t SPBRG; /* DAC baud rate register */
	__IO uint32_t CSR;   /* DAC current state register */
	__IO uint32_t LPR;   /* DAC loop register */ 
}dac0_reg_t;

/* GCR */
#define POWERDOWN _BIT(7)
#define TX_TRIG   _BIT(2)
#define DAC_EN    _BIT(0)

/* ISR/IER/ICR */
#define TX_UERR  _BIT(1)
#define TX_AVL   _BIT(0)

/* CSR */
#define TX_EMPTY _BIT(2)
#define TX_AVL   _BIT(1)
#define TX_FULL  _BIT(0)

/* LPR */
#define LOOP_START _BIT(1)
#define LOOP_EN    _BIT(0)

/* SPBRG */
#define SPBRG_MAX 0x3fff
#define SPBRG_MIN 4

#define DAC0     ((dac0_reg_t *)(T1_DAC_BASE))

#define dac_on()  do{DAC0->GCR &= ~POWERDOWN; DAC0->GCR |= DAC_EN;}while(0)
#define dac_off() do{DAC0->GCR &= ~DAC_EN; DAC0->GCR |= POWERDOWN;}while(0)

typedef enum {
    DAC_LOOP_EN,
    DAC_LOOP_DIS,
}dac_loop_t;

typedef enum {
    DAC_INT_EN,
    DAC_INT_DIS,
}dac_int_t;

extern int dac_init(uint32_t baud, dac_loop_t loop_status, uint32_t int_status);
extern void dac_output(uint8_t *dacBuf, uint32_t dacBufLen);
//extern void dac_output(uint16_t *dacBuf, uint32_t dacBufLen);
extern void dac_stop(void);

#endif
